use clap::arg;
use clap::Args;
use fudata::db;
use log::debug;
use log::error;
use log::info;
use tokio::select;
use anyhow::Result;
use anyhow::anyhow;

use crate::qtrade::strategy;

#[derive(Args, Debug)]
pub struct QbtCommandArguments {
    #[arg(
        help = "Database path used for storing data",
        short = 'p',
        long = "path",
        requires = "path",
        default_value = "memory"
    )]
    pub path: String,

    #[arg(
        help = "settings to craw securities",
        short = 's',
        long = "settings",
        default_value = "./config.toml"
    )]
    /// settings in toml format, e.g. config.toml
    pub settings: String,
}

pub async fn init(QbtCommandArguments { path, settings, .. }: QbtCommandArguments) -> Result<()> {
    let dbrs = db::init().await;
    match dbrs {
        Ok(_) => {}
        Err(e) => {
            return Err(anyhow!(e));
        }
    }

    info!("Starting fudacraw qbt...");

    debug!("(qbt::init) arguments: path= {}", path);
    debug!("(qbt::init) arguments: settings=  {}", settings);

    // let strategies = StrategyContainer::new();
    let securities = vec!["AAPL", "GOOGL", "MSFT"];
    let rs = strategy::run_backtests(&securities).await?;

    // All ok
    Ok(())
}
